package com.ganzi.ganziitem.rabbit;

import com.ganzi.ganziitem.core.Information;
import com.ganzi.ganziitem.netty.NettyServerListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.*;

import static java.awt.SystemColor.info;

@Service
public class SendUtil {
    private static Logger log = LoggerFactory.getLogger(SendUtil.class);

    @Value("${rabbit.sendKey}")
    private String sendKey;
    @Value("${rabbit.sendExchange}")
    private String sendExchange;
    @Value("${rabbit.shoolQueue}")
    private String shoolQueue;
    @Value("${rabbit.sendStateKey}")
    private String sendStateKey;

    @Autowired
    private RabbitTemplate rabbitTemplate;


    public void sendMessageToServer(Information info)throws Exception{
        CorrelationData correlationData = new CorrelationData(UUID.randomUUID().toString());
        MessagePostProcessor processor = new MessagePostProcessor(){
            @Override
            public Message postProcessMessage(Message message) throws AmqpException {
                message.getMessageProperties().setExpiration("10000");//过期10秒
                return message;
            }
        };
        rabbitTemplate.convertAndSend(sendExchange, sendKey, info, processor,correlationData);
        log.info("发送消息："+info);
    }

    public void sendStateToServer(String ip,int schoolState,int state){
        CorrelationData correlationData = new CorrelationData(UUID.randomUUID().toString());
        MessagePostProcessor processor = new MessagePostProcessor(){
            @Override
            public Message postProcessMessage(Message message) throws AmqpException {
                message.getMessageProperties().setExpiration("10000");//过期10秒
                return message;
            }
        };
        Map map = new HashMap<String,String>();
        map.put("channelName",shoolQueue);
        map.put("schoolState",schoolState);
        map.put("ip",ip);
        map.put("state",state);
        rabbitTemplate.convertAndSend(sendExchange, sendStateKey, map, processor,correlationData);
        log.info("发送在线状态消息："+map);
    }

    public void sendCollectionStateToServer(){
        CorrelationData correlationData = new CorrelationData(UUID.randomUUID().toString());
        MessagePostProcessor processor = new MessagePostProcessor(){
            @Override
            public Message postProcessMessage(Message message) throws AmqpException {
                message.getMessageProperties().setExpiration("10000");//过期10秒
                return message;
            }
        };
        List<String> iplist = new ArrayList<>();
        iplist.addAll(NettyServerListener.channelMap.keySet());
        if(iplist.size()<=0)return;
        Map map = new HashMap<String,String>();
        map.put("channelName",shoolQueue);
        map.put("schoolState",1);
        map.put("ipList",iplist);
        map.put("state",1);
        rabbitTemplate.convertAndSend(sendExchange, sendStateKey, map, processor,correlationData);
        log.info("发送在线状态消息："+map);
    }

}
